lib: Add ostree_sysroot_init_osname() API, bump mtime
authorColin Walters <walters@verbum.org>
Thu, 3 Mar 2016 16:39:33 +0000 (11:39 -0500)
committerColin Walters <walters@verbum.org>
Thu, 3 Mar 2016 19:21:57 +0000 (14:21 -0500)
And change the command line to use it.  rpm-ostree had a copy
of this code, and thus there's a clear reason to have an API.

While we're moving this into API, ensure the mtime on deploy is bumped
after an osname is created, so that daemons like rpm-ostree can notice
changes.  (In reality, creating the directory should do this, but
let's be double sure)

apidoc/ostree-sections.txt
src/libostree/libostree.sym
src/libostree/ostree-sysroot-deploy.c
src/libostree/ostree-sysroot-private.h
src/libostree/ostree-sysroot.c
src/libostree/ostree-sysroot.h
src/ostree/ot-admin-builtin-os-init.c

index e4ce525d53d154cfb61d61acf6f5c7ce2c573e7c..ee4ba1c6e792940c4fd9f77d792fe1d1dc7eac46 100644 (file)
@@ -397,6 +397,7 @@ ostree_sysroot_get_deployment_origin_path
 ostree_sysroot_cleanup
 ostree_sysroot_prepare_cleanup
 ostree_sysroot_get_repo
+ostree_sysroot_init_osname
 ostree_sysroot_deployment_set_kargs
 ostree_sysroot_write_deployments
 ostree_sysroot_deploy_tree
index f86a1ead0fd43925cde089cfa8e7480ea9e830a1..03350176a9c883800420161c80434f6a0f18fb99 100644 (file)
@@ -316,4 +316,5 @@ LIBOSTREE_2016.4 {
 global:
         ostree_repo_get_dfd;
         ostree_repo_list_refs_ext;
+        ostree_sysroot_init_osname;
 } LIBOSTREE_2016.3;
index b4f7cd27d278000f9f9f96c099a6de575ac56fbf..1f15ddcfa775b25f0602d0c58c88ce739bda0c8b 100644 (file)
@@ -1855,12 +1855,8 @@ ostree_sysroot_write_deployments (OstreeSysroot     *self,
                                 requires_new_bootversion ? "yes" : "no",
                                 new_deployments->len - self->deployments->len);
 
-  /* Allow other systems to monitor for changes */
-  if (utimensat (self->sysroot_fd, "ostree/deploy", NULL, 0) < 0)
-    {
-      glnx_set_prefix_error_from_errno (error, "%s", "futimens");
-      goto out;
-    }
+  if (!_ostree_sysroot_bump_mtime (self, error))
+    goto out;
 
   /* Now reload from disk */
   if (!ostree_sysroot_load (self, cancellable, error))
index e0dc24fd071029044aa5f9a59c02d23f7de92ba8..356509adfcc9599a44872bb50b2a7881034e28a4 100644 (file)
@@ -98,6 +98,9 @@ gboolean _ostree_sysroot_query_bootloader (OstreeSysroot     *sysroot,
                                            GCancellable      *cancellable,
                                            GError           **error);
 
+gboolean _ostree_sysroot_bump_mtime (OstreeSysroot *sysroot,
+                                     GError       **error);
+
 typedef enum {
   OSTREE_SYSROOT_CLEANUP_BOOTVERSIONS = 1 << 0,
   OSTREE_SYSROOT_CLEANUP_DEPLOYMENTS  = 1 << 1,
index 5ad2713ae83e40d425335fd3b107e1b5568b23c4..eb2b353db55059303b8dd048726dfcd8443f2c0d 100644 (file)
@@ -229,6 +229,19 @@ ostree_sysroot_get_fd (OstreeSysroot *self)
   return self->sysroot_fd;
 }
 
+gboolean
+_ostree_sysroot_bump_mtime (OstreeSysroot *self,
+                            GError       **error)
+{
+  /* Allow other systems to monitor for changes */
+  if (utimensat (self->sysroot_fd, "ostree/deploy", NULL, 0) < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "%s", "futimens");
+      return FALSE;
+    }
+  return TRUE; 
+}
+
 /**
  * ostree_sysroot_unload:
  * @self: Sysroot
@@ -1337,6 +1350,86 @@ ostree_sysroot_lock_finish (OstreeSysroot         *self,
   return g_task_propagate_boolean ((GTask*)result, error);
 }
 
+/**
+ * ostree_sysroot_init_osname:
+ * @self: Sysroot
+ * @osname: Name group of operating system checkouts
+ * @cancellable: Cancellable
+ * @error: Error
+ * 
+ * Initialize the directory structure for an "osname", which is a
+ * group of operating system deployments, with a shared `/var`.  One
+ * is required for generating a deployment.
+ */
+gboolean
+ostree_sysroot_init_osname (OstreeSysroot       *self,
+                            const char          *osname,
+                            GCancellable        *cancellable,
+                            GError             **error)
+{
+  gboolean ret = FALSE;
+  const char *deploydir = glnx_strjoina ("ostree/deploy/", osname);
+  glnx_fd_close int dfd = -1;
+
+  if (!ensure_sysroot_fd (self, error))
+    goto out;
+
+  if (mkdirat (self->sysroot_fd, deploydir, 0777) < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "Creating %s", deploydir);
+      goto out;
+    }
+
+  if (!glnx_opendirat (self->sysroot_fd, deploydir, TRUE, &dfd, error))
+    goto out;
+
+  if (mkdirat (dfd, "var", 0777) < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "Creating %s", "var");
+      goto out;
+    }
+
+  /* This is a bit of a legacy hack...but we have to keep it around
+   * now.  We're ensuring core subdirectories of /var exist.
+   */
+  if (mkdirat (dfd, "var/tmp", 0777) < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "Creating %s", "var/tmp");
+      goto out;
+    }
+
+  if (fchmodat (dfd, "var/tmp", 01777, 0) < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "Fchmod %s", "var/tmp");
+      goto out;
+    }
+
+  if (mkdirat (dfd, "var/lib", 0777) < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "Creating %s", "var/tmp");
+      goto out;
+    }
+
+  if (symlinkat ("../run", dfd, "var/run") < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "Symlinking %s", "var/run");
+      goto out;
+    }
+
+  if (symlinkat ("../run/lock", dfd, "var/lock") < 0)
+    {
+      glnx_set_prefix_error_from_errno (error, "Symlinking %s", "var/lock");
+      goto out;
+    }
+
+  if (!_ostree_sysroot_bump_mtime (self, error))
+    goto out;
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
 /**
  * ostree_sysroot_simple_write_deployment:
  * @sysroot: Sysroot
index bce8698590cd0d2d3e10a9e95effebe333b50647..cab59d0a6ecf7bf8772b82e9420d2c230829d445 100644 (file)
@@ -97,6 +97,12 @@ gboolean ostree_sysroot_lock_finish (OstreeSysroot         *self,
 _OSTREE_PUBLIC
 void ostree_sysroot_unlock (OstreeSysroot  *self);
 
+_OSTREE_PUBLIC
+gboolean ostree_sysroot_init_osname (OstreeSysroot       *self,
+                                     const char          *osname,
+                                     GCancellable        *cancellable,
+                                     GError             **error);
+
 _OSTREE_PUBLIC
 gboolean ostree_sysroot_cleanup (OstreeSysroot       *self,
                                  GCancellable        *cancellable,
index f5b5f64b1011233934eb0d532842f7bd0605d32e..fbb049493ea68a380ee26ff0296c6b32c55933a7 100644 (file)
@@ -40,8 +40,6 @@ ot_admin_builtin_os_init (int argc, char **argv, GCancellable *cancellable, GErr
   glnx_unref_object OstreeSysroot *sysroot = NULL;
   gboolean ret = FALSE;
   const char *osname = NULL;
-  g_autoptr(GFile) deploy_dir = NULL;
-  g_autoptr(GFile) dir = NULL;
 
   context = g_option_context_new ("OSNAME - Initialize empty state for given operating system");
 
@@ -61,48 +59,10 @@ ot_admin_builtin_os_init (int argc, char **argv, GCancellable *cancellable, GErr
 
   osname = argv[1];
 
-  deploy_dir = ot_gfile_get_child_build_path (ostree_sysroot_get_path (sysroot), "ostree", "deploy", osname, NULL);
-
-  /* Ensure core subdirectories of /var exist, since we need them for
-   * dracut generation, and the host will want them too.
-   */
-  g_clear_object (&dir);
-  dir = ot_gfile_get_child_build_path (deploy_dir, "var", "tmp", NULL);
-  if (!gs_file_ensure_directory (dir, TRUE, cancellable, error))
-    goto out;
-  if (chmod (gs_file_get_path_cached (dir), 01777) < 0)
-    {
-      gs_set_error_from_errno (error, errno);
-      goto out;
-    }
-
-  g_clear_object (&dir);
-  dir = ot_gfile_get_child_build_path (deploy_dir, "var", "lib", NULL);
-  if (!gs_file_ensure_directory (dir, TRUE, cancellable, error))
+  if (!ostree_sysroot_init_osname (sysroot, osname, cancellable, error))
     goto out;
 
-  g_clear_object (&dir);
-  dir = ot_gfile_get_child_build_path (deploy_dir, "var", "run", NULL);
-  if (!g_file_test (gs_file_get_path_cached (dir), G_FILE_TEST_IS_SYMLINK))
-    {
-      if (symlink ("../run", gs_file_get_path_cached (dir)) < 0)
-        {
-          gs_set_error_from_errno (error, errno);
-          goto out;
-        }
-    }
-
-  dir = ot_gfile_get_child_build_path (deploy_dir, "var", "lock", NULL);
-  if (!g_file_test (gs_file_get_path_cached (dir), G_FILE_TEST_IS_SYMLINK))
-    {
-      if (symlink ("../run/lock", gs_file_get_path_cached (dir)) < 0)
-        {
-          gs_set_error_from_errno (error, errno);
-          goto out;
-        }
-    }
-
-  g_print ("%s initialized as OSTree root\n", gs_file_get_path_cached (deploy_dir));
+  g_print ("ostree/deploy/%s initialized as OSTree root\n", osname);
 
   ret = TRUE;
  out: